/*

	Write a program that takes two arguments at the command line, both strings.
	The program checks to see whether or not the second string is a substring
	of the first (without using the substr -- or any other library -- function).
	One caveat: any * in the second string can match zero or more characters
	in the first string, so if the input were abcd and the substring were a*c,
	then it would count as a substring. Also, include functionality to allow
	an asterisk to be taken literally if preceded by a \, and a \ is taken
	literally except when preceding an asterisk.


0 1 2 3 4 5 6 7
a b c d e f g \0

*g\0
. . . . . . g


*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

void help(){
	cout << "regexp <string> <maska>" << endl;
}

void skonci(int rv){
	exit(rv);
}

int main( int argc, char* argv[] ){
	if (!argv[2]) {
		help();
		exit(EXIT_FAILURE);
  }

	cout << "String: " << argv[1] << " Maska: " << argv[2] << endl;


	char *psrc = argv[1];
	char *pmaska = argv[2];

	cout << *psrc << "\t" << *pmaska << endl;
	while (*psrc != NULL) {


		if ( *psrc != *pmaska) {			// znak zdroja je iny, ako znak masky?
			if (*pmaska == '*' )	{				// je maska hviezdicka?
				if (*(pmaska+1) == *psrc || *(psrc+1) == *(pmaska+1) ) // je to dalsi znak masky?
					pmaska++;
				psrc++;
			}
			else                        // Ak nie, tak rovno skoci, lebo to znamena mismatch
				skonci(1);
		}
		else {                        // znak zdroja sedi so znakom masky
			 psrc++;                    // posun oba ukazovatele
			 pmaska++;
		}
		cout << *psrc << "\t" << *pmaska << endl;
	}

}
